
import { createI18n,useI18n  } from 'vue-i18n';
import zhLocale from './locales/zh.json'
import enLocale from './locales/en.json'
import twLocale from './locales/tw.json'
import elEnLocale from 'element-plus/es/locale/lang/en';
import elZhLocale from 'element-plus/es/locale/lang/zh-cn';
import twTcLocale from 'element-plus/es/locale/lang/zh-tw'

export function getLanguage() {
    const chooseLanguage = localStorage.getItem('lang')
    if (chooseLanguage) return chooseLanguage
    const language = (navigator.language).toLowerCase()
    const locales = Object.keys(messages)
    for (const locale of locales) {
        if (language.indexOf(locale) > -1) {
            return locale
        }
    }
    return 'zh'
}
const messages = {
    en: {
        ...enLocale,
        ...elEnLocale
    },
    zh: {
        ...zhLocale,
        ...elZhLocale
    },
    tw: {
        ...twLocale,
        ...twTcLocale
    }
}
const i18n = createI18n({
    locale: getLanguage(),
    legacy: false,
    globalInjection: true, // 全局模式，可以直接使用 $t
    messages: messages
})
export default i18n